package com.bomberdroid.controller;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.util.Log;

import com.bomberdroid.R;
import com.bomberdroid.model.Player;
import com.bomberdroid.protocols.DataSharing;

public class GameJoystick {

	private static final String TAG = GameJoystick.class.getSimpleName();

	private Player player;
	private int indicePlayer;

	private Bitmap _joystick;
	private Bitmap _joystickBg;
	private Bitmap _joystickBgActif;
	private int joystickHalfSize;
	private int joystickBgHalfSize;

	private boolean actif;

	private static Point origJoystickPoint;
	public Point joystickPoint;

	private int seuilJoystick = 250;

	private static final int ZERO = 0;
	private static final int UP = 1;
	private static final int RIGHT = 2;
	private static final int DOWN = 3;
	private static final int LEFT = 4;
	private static final int UP_RIGHT = 5;
	private static final int DOWN_RIGHT = 6;
	private static final int DOWN_LEFT = 7;
	private static final int UP_LEFT = 8;

	private int etatJoystick = 0;

	public GameJoystick(Resources res, Player p, int width, int height) {

		Log.i(TAG, "Constructeur " + TAG);
		_joystick = (Bitmap) BitmapFactory.decodeResource(res, R.drawable.joystick);
		_joystickBg = (Bitmap) BitmapFactory.decodeResource(res, R.drawable.joystick_bg);
		_joystickBgActif = (Bitmap) BitmapFactory.decodeResource(res, R.drawable.joystick_bg_actif);

		joystickHalfSize = (int) _joystick.getHeight() / 2;
		joystickBgHalfSize = (int) _joystickBg.getHeight() / 2;

		player = p;
		
		origJoystickPoint = new Point((int)(joystickBgHalfSize*1.6), height-(int)(joystickBgHalfSize*1.6));
		joystickPoint = new Point(origJoystickPoint);

	}

	public int getJoystickSize() {
		return joystickHalfSize;
	}

	public int getJoystickBgSize() {
		return joystickBgHalfSize;
	}

	public Point getOrigine() {
		// Log.i(TAG,"x :" + origJoystickPoint.x + "   y : " +
		// origJoystickPoint.y);
		return origJoystickPoint;
	}

	public int getSeuil() {
		return seuilJoystick;
	}

	public Bitmap get_joystick() {
		return _joystick;
	}

	public void setPlayer(Player player) {
		this.player = player;
	}

	public void setIndicePlayer(int indicePlayer) {
		this.indicePlayer = indicePlayer;
	}

	public Bitmap get_joystickBg() {
		if (actif)
			return _joystickBgActif;
		else
			return _joystickBg;
	}

	void onTouchJoystick(Point touch) {

		int direcTouch;
		if (!actif) {
			origJoystickPoint = new Point(touch);
			actif = true;
		}

		double dist = Math.sqrt(Math.pow(touch.x - origJoystickPoint.x, 2) + Math.pow(touch.y - origJoystickPoint.y, 2));

		if (dist == 0) {
			direcTouch = ZERO;
		}

		else {
			if (dist > 70) {

				// Joystick out of range
				// We put it on the max range circle
				joystickPoint.x = (int) origJoystickPoint.x + ((touch.x - origJoystickPoint.x) * 70 / (int) dist);
				joystickPoint.y = (int) origJoystickPoint.y + ((touch.y - origJoystickPoint.y) * 70 / (int) dist);
			} else
				joystickPoint = touch;

			// get the angle
			double angle = Math.atan2(origJoystickPoint.y - joystickPoint.y, origJoystickPoint.x - joystickPoint.x)
					/ (Math.PI / 180);

			if (angle < -155 || angle > 155) {
				direcTouch = RIGHT;
			} else if (angle < -115) {
				direcTouch = DOWN_RIGHT;
			} else if (angle < -65) {
				direcTouch = DOWN;
			} else if (angle < -25) {
				direcTouch = DOWN_LEFT;
			} else if (angle < 25) {
				direcTouch = LEFT;
			} else if (angle < 65) {
				direcTouch = UP_LEFT;
			} else if (angle < 115) {
				direcTouch = UP;
			} else {
				direcTouch = UP_RIGHT;
			}
		}

		updateState(direcTouch);
		// Log.i(TAG, "JOYSTICK " +etatJoystick);
	}

	void inactif() {
		if (!actif)
			return;

		updateState(ZERO);
		actif = false;
	}

	int getEtat() {
		return etatJoystick;
	}

	void updateState(int etat) {

		if (etat == etatJoystick)
			return;
		
		DataSharing.message += "1;"+indicePlayer+";"+etat+"&";
		this.player.updateState(etat);
		
		etatJoystick = etat;
	}

	public void onDraw(Canvas canvas) {

		if (!actif) return;
		
		canvas.drawBitmap(get_joystickBg(), origJoystickPoint.x - joystickBgHalfSize, origJoystickPoint.y - joystickBgHalfSize,
				null);

		canvas.drawBitmap(_joystick, joystickPoint.x - joystickHalfSize, joystickPoint.y - joystickHalfSize, null);
	}
}
